package com.example.edianzu.somur.utils.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.edianzu.somur.R;
import com.example.edianzu.somur.utils.content.L;
import com.trello.rxlifecycle2.components.support.RxFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * author：风不会停息 on 2017/7/22 18:38
 * mailbox：yh131412hys@163.com
 * project：Somur
 * tel：13295355900
 * describe：
 */

public abstract class BaseFragment extends RxFragment{

    public TabLayout tabLayout;
    public ViewPager viewPager;
    private List<String> title;//标题集合
    private List<Fragment> fragmentList;//该fragment里面需要嵌入的子fragment集合
    private View view;


    /**
     * 在方法中对上述两个集合进行初始化
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = new ArrayList<>();//实例化标题集合
        fragmentList = new ArrayList<>();//实例化子fragment集合
        initViewPagerFragment(title, fragmentList);
    }


    /**
     * 该方法由子类继承,并传入数据源
     *
     * @param title
     * @param fragmentList
     */
    protected abstract void initViewPagerFragment(List<String> title, List<Fragment> fragmentList);

    protected abstract boolean isLoadTopView();

    protected abstract int setTopViewId();

    protected abstract int setLoadBottomViewId();

    protected abstract boolean isLoadBottomView();

    public void loadBottomView() {
        if (isLoadBottomView()) {
            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.content_view);

            View bottomView = View.inflate(getContext(), setLoadBottomViewId(), null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);//定义框架布局器参数

            // params.gravity = Gravity.BOTTOM;
            linearLayout.addView(bottomView);

        }
    }

    private void loadTopView() {
        if (isLoadTopView()) {
            LinearLayoutCompat linearLayout = (LinearLayoutCompat) view.findViewById(R.id.content_view);
            View topView = View.inflate(getContext(), setTopViewId(), null);
            linearLayout.addView(topView, 0, new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT));

        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.fragment_base, container, false);
        }
        loadTopView();
        loadBottomView();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) view.findViewById(R.id.f_pager);
        tabLayout = (TabLayout) view.findViewById(R.id.f_tab);

        //嵌套在fragment中的fragment管理为getChildFragmentManager()
//        MyFragmentAdapter adapter = new MyFragmentAdapter(getChildFragmentManager());
//        viewPager.setAdapter(adapter);
//        tabLayout.setupWithViewPager(viewPager);
//        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//        viewPager.setPageTransformer(true, new ZoomOutPageTransformer());//设置切换动画


    }

    public void setTabLayoutMode(int modeType) {
        tabLayout.setTabMode(modeType);
    }

    /**
     * 子fragment适配器
     */
    public class MyFragmentAdapter extends FragmentPagerAdapter {

        public MyFragmentAdapter(FragmentManager fm) {

            super(fm);

        }

        @Override
        public Fragment getItem(int position) {

            return fragmentList.get(position);


        }


        @Override
        public int getCount() {
            return title.size();

        }

        @Override
        public CharSequence getPageTitle(int position) {

            return title.get(position);
        }

        public void setPageTitle(List<String> list){
            title.clear();
            title.addAll(list);
            notifyDataSetChanged();
        }


    }

    @Override
    public void onDestroyView() {
        if (view !=null){
            L.e("onDestroyView");
            ((ViewGroup)view.getParent()).removeView(view);
        }
        super.onDestroyView();
    }
}
